/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.univaq.iw.jweb;
import it.univaq.iw.jweb.data.Curriculum;
import it.univaq.iw.jweb.data.DataAccess;
import it.univaq.iw.jweb.utils.HTMLHelpers;
import it.univaq.iw.jweb.utils.TemplateManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AutoComplete extends HttpServlet {

    private DataAccess da;
    private TemplateManager tpl;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        //inzializziamo il layer di accesso ai dati
        da = DataAccess.getInstance();
        //inzializziamo il layer di presentazione (gestione template)
        tpl = TemplateManager.getInstance(getServletContext());
        tpl.setOutlineTemplate("outline");
        tpl.setDefaultData("author", "Del Vecchio - Bevini - Delfino");
        tpl.setDefaultData("homepage_url", "home");
        tpl.setDefaultData("logged", true);
    }

private void makeJSONResultPage(Set<String> result, PrintWriter out) {
        //prendiamo il data model di default
        Map datamodel = tpl.getDefaultDataModel();
        datamodel.put("resultList", result);
        try {
            //compiliamo il template
            tpl.processJSON("autoComplete.ftl.json", datamodel, out);
            } catch (ServletException ex) {
            //in caso di errore, eseguiamo la routine associata
            HTMLHelpers.makeErrorPage(tpl, ex.getMessage(), out);
        }
    }


          protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          if (request.getParameter("json") != null) {
              response.setContentType("text/plain;charset=UTF-8");
          } else {
              response.setContentType("text/html;charset=ISO-8859-1");
          }
          PrintWriter out = response.getWriter();

          //preleviamo la stringa di ricerca dal parametro q
          String queryString = request.getParameter("p");
          //se la stringa è valida...
          if (queryString != null && !queryString.isEmpty() && !queryString.equalsIgnoreCase(" ") && queryString.length()>=2) {
              //chiediamo al layer dati i profili corrispondenti
              Set<String> result = da.searchSettoreStudio(queryString);
              makeJSONResultPage(result, out);
          } else {
              HTMLHelpers.makeErrorPage(tpl, "Parametro di ricerca non specificato", out);
          }
      }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

  }


